//
//  LYCTabBar.swift
//  新浪微博
//
//  Created by liuyanchi on 16/7/29.
//  Copyright © 2016年 liuyanchi. All rights reserved.
//

import UIKit

class LYCTabBar: UITabBar {
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        // 添加子控件
        
        addSubview(compose2Button)
    }
    
    
    override func layoutSubviews() {
        
        // 调用父类
        super.layoutSubviews()
        
        let width = bounds.width / 5
        let height = bounds.height
        
        var index = 0
        
        for subview in subviews {
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                printLog("创建的就是这个")
                
                subview.frame = CGRectMake(CGFloat(index) * width, bounds.origin.y, width, height)
                
                index++
                
                if index == 2{index++}
                
            }
            
            compose2Button.frame = CGRectMake(2 * width, bounds.origin.y, width, height)
        }
    }
    
    // 创建按钮的点击事件
    func click(){
        
        print("添加按钮点击了")
        
        
    }
    
    
    
    // 懒加载创建中间的item
    private lazy var composeButton: UIButton = UIButton()
    
    private lazy var compose2Button: UIButton = {
        
        let button = UIButton()
        
        // 设置属性
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加点击事件
        button.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        
        return button
        
    }()
    
        
}
